<?php
require_once '../dao/QuestionDao.php';
require_once '../entity/Question.php';

$action = "";
if (isset($_GET['action'])) {
	$action = $_GET['action'];
}

switch ($action) {
	case 'maskerQuestions' :
		maskerQuestions();
		break;
	case 'insertQuestion' :
		insertQuestion();
		break;
	case 'updateQuestionAnswer' :
		updateQuestionAnswer();
		break;
	case 'deleteQuestion' :
		deleteQuestion();
		break;
	default :
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no action found";
		print json_encode($jsonResult);
		break;
}


/*
 * Permit to retrieve questions of a masker
 */
function maskerQuestions() {
	$questionDao = new QuestionDao();
	$jsonResult = array();
	$lastId = null;

	if (isset($_POST['last_id'])) {
		$lastId = $_POST['last_id'];
	}

	// check if name masker is isset
	if (!isset($_POST['masker'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no masker found";
	} else {
		$masker = $_POST['masker'];
		$value = $questionDao -> getMaskerQuestion($masker, $lastId);
		$jsonResult['value'] = ($value);
	}
	print json_encode($jsonResult);
}

/*
 * Permit to insert a question
 */
function insertQuestion() {
	$questionDao = new QuestionDao();
	
	$jsonResult = array();

	// check if text and id_masker are isset
	if (!isset($_POST['id_masker_from'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id_masker_from found";
	} else if (!isset($_POST['name_masker_to'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no name_masker_to found";
	} else if ($_POST['text'] == '') {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no empty text";
	} else if (strlen ($_POST['text']) > 500) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "text too long";
	} else if (strlen ($_POST['text']) == 0) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "text too short";
	} else {
		$text = $_POST['text'];
		$idMaskerFrom = $_POST['id_masker_from'];
		$nameMaskerTo = $_POST['name_masker_to'];
		
		if(isset($_POST['id_sign'])) {
			$signId = $_POST['id_sign'];
		} else {
			$signId = null;
		}
		
		if(isset($_POST['text_answer'])) {
			$textAnswer = $_POST['text_answer'];
		} else {
			$textAnswer = null;
		}

		$result = $questionDao -> insertQuestion($idMaskerFrom, $nameMaskerTo, $signId, $text, $textAnswer);
		$jsonResult['result'] = ($result);
	}

	print json_encode($jsonResult);
}

/*
 * Permit to insert an answer of a question
 */
function updateQuestionAnswer() {
	$questionDao = new QuestionDao();
	
	$jsonResult = array();

	// check if text and id_masker are isset
	if (!isset($_POST['id_masker'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id_masker found";
	} else if (!isset($_POST['id_question'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id_question found";
	} else if ($_POST['text_answer'] == '') {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no empty text";
	} else if (strlen ($_POST['text_answer']) > 500) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "text too long";
	} else if (strlen ($_POST['text_answer']) == 0) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "text too short";
	} else {
		$textAnswer = $_POST['text_answer'];
		$idQuestion = $_POST['id_question'];
		$idMasker = $_POST['id_masker'];

		$result = $questionDao -> updateQuestionAnswer($idQuestion, $idMasker, $textAnswer);
		$jsonResult['result'] = ($result);
	}

	print json_encode($jsonResult);
}

/*
 * Permit a logical question delete 
 */
function deleteQuestion() {
	$questionDao = new QuestionDao();
	
	$jsonResult = array();

	// check if id_masker and id_question are isset
	if (!isset($_POST['id_masker'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id_masker found";
	} else if (!isset($_POST['id_question'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id_question found";
	} else {
		$idQuestion = $_POST['id_question'];
		$idMasker = $_POST['id_masker'];
		

		$result = $questionDao -> deleteQuestion($idQuestion, $idMasker);
		$jsonResult['result'] = ($result);
	}

	print json_encode($jsonResult);
}
?>